package com.icss.th.question.facade.mongo;

import java.util.ArrayList;
import java.util.List;

import com.icss.th.common.util.StringUtil;
import com.icss.th.db.MongoConnection;
import com.icss.th.question.facade.IQuestionFacade;
import com.icss.th.question.pojo.Item;
import com.icss.th.question.pojo.Question;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

public class QuestionFacadeMongo implements IQuestionFacade 
{	
	@Override
	public String getBrotherId(String paperid, String sort, boolean next) 
	{
		String id = null;
		
		DBCollection collection = MongoConnection.getCollection("examdata");
		if (collection == null) return null;
		
		DBObject query = new BasicDBObject("inPaperId", Integer.valueOf(paperid))
		.append("inPaperSort", new BasicDBObject(next? "$gt" : "$lt", Integer.valueOf(sort)));
		DBObject fields = new BasicDBObject("_id", true);
		DBObject orderBy = new BasicDBObject("inPaperSort", next? 1 : -1);
		
		DBObject one = collection.findOne(query, fields, orderBy);
		if (one != null) id = String.valueOf(one.get("_id"));
		
		return id;
	}

	@Override
	public Question getQuestion(String id)
	{
		Question question = null;
		
		DBCollection collection = MongoConnection.getCollection("examdata");
		if (collection == null) return null;
		
		DBObject query = new BasicDBObject("_id", Integer.valueOf(id));
		DBObject fields = new BasicDBObject()
		.append("examType", true)
		.append("examTitle", true)
		.append("A", true)
		.append("B", true)
		.append("C", true)
		.append("D", true)
		.append("E", true)
		.append("F", true)
		.append("G", true)
		.append("PageUrl", true)
		.append("examAnswer", true)
		.append("examExplain", true)
		.append("inPaperId", true)
		.append("paperTitle", true)
		.append("inPaperSort", true)
		.append("keyType", true);
		
		DBObject one = collection.findOne(query, fields);
		if (one == null) return null;

		int examId = (Integer) one.get("_id");
		String examType = (String) one.get("examType");
		String examTitle = (String) one.get("examTitle");
		String A = (String) one.get("A");
		String B = (String) one.get("B");
		String C = (String) one.get("C");
		String D = (String) one.get("D");
		String E = (String) one.get("E");
		String F = (String) one.get("F");
		String G = (String) one.get("G");
		String pageUrl = (String) one.get("PageUrl");
		String examAnswer = (String) one.get("examAnswer");
		String examExplain = (String) one.get("examExplain");
		int inPaperId = (Integer) one.get("inPaperId");
		String paperTitle = (String) one.get("paperTitle");
		int paperSort = (Integer) one.get("inPaperSort");
		int keyType = (Integer) one.get("keyType");
		
		List<Item> items = new ArrayList<Item>();
		if (!StringUtil.isEmpty(A))	items.add(new Item("A", StringUtil.format(A)));
		if (!StringUtil.isEmpty(B))	items.add(new Item("B", StringUtil.format(B)));
		if (!StringUtil.isEmpty(C))	items.add(new Item("C", StringUtil.format(C)));
		if (!StringUtil.isEmpty(D))	items.add(new Item("D", StringUtil.format(D)));
		if (!StringUtil.isEmpty(E))	items.add(new Item("E", StringUtil.format(E)));
		if (!StringUtil.isEmpty(F))	items.add(new Item("F", StringUtil.format(F)));
		if (!StringUtil.isEmpty(G))	items.add(new Item("G", StringUtil.format(G)));
		
		question = new Question();
		question.setId(examId);
		question.setTitle(StringUtil.format(examTitle));
		question.setAnswer(StringUtil.format(examAnswer));
		question.setExplain(StringUtil.format(examExplain));
		question.setItems(items);
		question.setPaperName(StringUtil.format(paperTitle));
		question.setPaperId(inPaperId);
		question.setSourceUrl(StringUtil.format(pageUrl));
		question.setTypeName(StringUtil.format(examType));
		question.setPaperSort(paperSort);
		question.setTypeId(keyType);
		
		return question;
	}
}
